﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Daemon.Common.Generator
{
    public class SelectComponentGenerator : ComponentGenerator
    {
        public override string WidgetTypeName => "Select";

        public override ComponentResponse DoGenerate(Component component)
        {
            var response = new ComponentResponse();
            StringBuilder strResult = new StringBuilder();
            strResult.Append($"<Form.Item label='{component.Title}' name='{component.Name}' ");
            if (component.Required)
            {
                strResult.Append("rules={[{ required: true, message: '" + component.Message + "' }]}");
            }
            strResult.Append(" >");
            strResult.Append($"<{WidgetTypeName} placeholder='{component.Placeholder}' mode='{component.Mode}' name='{component.Name}' options={{selectOption}}");
            strResult.Append(" />");
            strResult.Append($"</Form.Item>");
            response.ComponentHtml = strResult.ToString();
            response.VariableList = GenerateVariable(component);
            return response;
        }

        public override List<string> GenerateVariable(Component component)
        {
            var selectOptions = new StringBuilder(" const selectOption:any[] = [");
            component.Props.Options.ForEach(option =>
            {
                if (int.TryParse(option.Value, out int _))
                {
                    selectOptions.Append($"{{value:{option.Value},label:'{option.Label}'}}");
                }
                else
                {
                    selectOptions.Append($"{{value:'{option.Value}',label:'{option.Label}}}'");
                }
            });
            selectOptions.Append("]  = useState<any>();");
            var result = new List<string>();
            result.Add(selectOptions.ToString());
            return result;
        }
    }
}
